package net.qiqbframework.eventhandling;

import net.qiqbframework.commandhandling.MessageHandlerInterceptorSupport;
import net.qiqbframework.messaging.MessageHandlerInterceptor;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 事件处理器处理来自事件队列或事件总线的事件消息。
 * 通常，事件处理器负责将事件发布到一组已注册的处理程序。这允许属性和行为（例如事务管理、异步处理、分发）一次应用于整个组
 */
public interface EventProcessor extends MessageHandlerInterceptorSupport<EventMessage<?>> {


    String getName();


    List<MessageHandlerInterceptor<? super EventMessage<?>>> getHandlerInterceptors();


    void start();


    void shutDown();


    boolean isRunning();

    /**
     * Indicates whether the processor has been shut down due to an error. In such case, the processor has forcefully
     * shut down, as it wasn't able to automatically recover.
     * <p>
     * Note that this method returns {@code false} when the processor was stopped using {@link #shutDown()}.
     *
     * @return {@code true} when paused due to an error, otherwise {@code false}
     */
    boolean isError();

    /**
     * Initiates a shutdown, providing a {@link CompletableFuture} that completes when the shutdown process is
     * finished.
     *
     * @return a CompletableFuture that completes when the shutdown process is finished.
     */
    default CompletableFuture<Void> shutdownAsync() {
        return CompletableFuture.runAsync(this::shutDown);
    }
}
